package com.liziba.rpc;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @auther LiZiBa
 * @date 2020/10/19 22:50
 * @description:
 **/
public class RpcNetTransport {

    private String host;
    private int port;

    public RpcNetTransport(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public Object send(RpcRequest rpcRequest) {

        // 客户端发起连接请求，服务端连接阻塞释放
        try(
                Socket socket =  new Socket(host, port);
                ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            ) {
            outputStream.writeObject(rpcRequest);
            outputStream.flush();

            // 使用在获取，免得导致阻塞
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            Object o = inputStream.readObject();
            return o;
        }catch (Exception e) {
            e.printStackTrace();

        }finally {
            // 关流
        }

        return null;
    }
}
